package com.soft.javase.day10.practice.collectorsapi;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author yzg
 * @desc TODO
 * @date 2025/5/4
 */
public class CollectorsDemo {
    public static void main(String[] args) {
        // 1. 将英文字符串数组中的元素全部变为大写，以集合输出
        String[] stringArray = {"apple", "banana", "orange"};
        List<String> upperCaseList = Arrays.stream(stringArray)
                .map(String::toUpperCase)
                .collect(Collectors.toList());
        System.out.println("大写字符串集合: " + upperCaseList);

        // 将整数数组中的元素全部加3，以集合输出
        Integer[] intArray = {1, 2, 3, 4, 5};
        List<Integer> addList = Arrays.stream(intArray)
                .map(n -> n + 3)
                .collect(Collectors.toList());
        System.out.println("加3后的整数集合: " + addList);


        List<Employee> employee = Arrays.asList(
                new Employee("张三", 7500.0),
                new Employee("李四", 8200.0),
                new Employee("王五", 6800.0),
                new Employee("小明", 9100.0),
                new Employee("小美", 7900.0)
        );

        // 统计员工人数
        long count = employee.stream().collect(Collectors.counting());
        System.out.println("员工人数: " + count);

        // 平均工资
        double avgSalary = employee.stream()
                .collect(Collectors.averagingDouble(Employee::getSalary));
        System.out.println("平均工资: " + avgSalary);

        // 工资总额
        double totalSalary = employee.stream()
                .collect(Collectors.summingDouble(Employee::getSalary));
        System.out.println("工资总额: " + totalSalary);

        // 最高工资
        Optional<Employee> maxSalary = employee.stream()
                .collect(Collectors.maxBy(Comparator.comparing(Employee::getSalary)));
        System.out.println("最高工资: " + maxSalary.map(Employee::getSalary).orElse(0.0));

        // 统计所有工资信息
        DoubleSummaryStatistics stats = employee.stream()
                .collect(Collectors.summarizingDouble(Employee::getSalary));
        System.out.println("工资统计: " + stats);


        //将员工按薪资是否高于8000分为两部分
        Map<Boolean, List<Employee>> partitionEmployee = employee.stream()
                .collect(Collectors.partitioningBy(e -> e.getSalary() > 8000));

        System.out.println("薪资高于8000的员工:"+partitionEmployee.get(true));
        System.out.println("薪资不高于8000的员工:"+partitionEmployee.get(false));


        //将所有员工的名称用逗号拼接
        System.out.println("员工名称: " + employee.stream()
                .map(Employee::getName)
                .collect(Collectors.joining(",")));
    }
}
